<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

require_once 'database.php';

class notif_handler extends database {

    public $id = null;
    public $sender = null;
    public $berita = null;
    public $read = null;
    public $photo_id = null;

    function  __construct() {
        parent::__construct();
    }

    function  __destruct() {

    }

    function retr_notification($usr_id){
        $query = "SELECT * FROM notification WHERE usr_tujuan='$usr_id' ORDER BY id DESC";

        //id
        $this->exec_q($query, "id");
        $i = $this->result;
        $this->id = $i;

        //sender
        $this->exec_q($query, "usr_pengirim");
        $s = $this->result;
        $this->sender = $s;

        //berita
        $this->exec_q($query, "berita");
        $b = $this->result;
        $this->berita = $b;

        //read
        $this->exec_q($query, "isread");
        $r = $this->result;
        $this->read = $r;

        //photo_id
        $this->exec_q($query, "photo_id");
        $p = $this->result;
        $this->photo_id = $p;
    }

    function add_notification($usr_kirim, $usr_tujuan, $berita, $read, $photo_id){
        $query = "INSERT INTO notification (usr_pengirim,usr_tujuan,berita,isread,photo_id)
                                    VALUES ('$usr_kirim','$usr_tujuan','$berita',$read,$photo_id)";

        $this->connect();
        mysql_query($query,$this->conn);
        mysql_close();
    }

    function set_read($id){
        $query = "UPDATE notification SET isread=1 WHERE id=$id";
        
        $this->connect();
        mysql_query($query,$this->conn);
        mysql_close();
    }

    function del_notification($id){
        $query = "DELETE FROM notification WHERE id=$id";

        $this->connect();
        mysql_query($query,$this->conn);
        mysql_close();
    }

    function set_follow_notif($usr_tujuan, $usr_kirim){
        //masukin record baru dengan user_pengirim=usr_kirim, usr_tujuan=usr_tujuan, berita=follow
        $this->add_notification($usr_kirim, $usr_tujuan, "follow", 0, 0);
    }

    function set_tag_notif($usr_tujuan, $usr_kirim, $photo_id){
        //masukin record baru dengan user_pengirim=usr_kirim, usr_tujuan=usr_tujuan, berita=tag
        $this->add_notification($usr_kirim, $usr_tujuan, "tag", 0, $photo_id);
    }

}
?>
